package com.hatic.contract;

import java.util.Map;

import org.jxstar.control.action.RequestContext;
import org.jxstar.dao.DaoParam;
import org.jxstar.service.BusinessObject;
import org.jxstar.util.StringUtil;

/**
 * 合同文本创建处理类
 *
 * @author 
 * 
 */
public class ContractCreateBO extends BusinessObject {
	private static final long serialVersionUID = 1L;

	/**
	 * 合同文本提交时根据项目类别来判断子功能是否必填
	 * @param contractIds
	 * @return
	 */
	public String preTextAudit(String[] contractIds,RequestContext request ) {
		if (contractIds == null || contractIds.length == 0) {
			setMessage("没有选择合同记录！");
			return _returnFaild;
		}
		String funId = request.getRequestValue("funid");
		Map<String,String> mpTable = queryTable(funId);
		String table_name = mpTable.get("table_name");
		
		for (int i = 0, n = contractIds.length; i < n; i++) {
			if (!checkContract(contractIds[i],table_name)) {
				return _returnFaild;
			}
		}
		
		return _returnSuccess;
	}
	
	//
	private boolean checkContract(String contractId,String table_name) {
		// TODO Auto-generated method stub
		Map<String,String> mp = queryContract(contractId,table_name);
		Map<String,String> mpdevice = queryDevice(contractId);
		Map<String,String> mpspare = querySpare(contractId);
		Map<String,String> mpservice = queryService(contractId);
		
		String money_type = mp.get("money_type");
		double cntdevice = StringUtil.tonum(mpdevice.get("cntdevice"));
		double cntspare = StringUtil.tonum(mpspare.get("cntspare"));
		double cntservice = StringUtil.tonum(mpservice.get("cntservice"));
		
		_log.showDebug("money_type:"+money_type+"cntdevice:"+cntdevice+"cntspare:"+cntspare+"cntservice:"+cntservice);
		if(money_type.equals("30")){
			if(cntspare == 0){
				setMessage("基建工程类的项目，必须填写合同主材清单！");
				return false;
			}
			
		}
		if (money_type.equals("1001")||money_type.equals("1002")||money_type.equals("1003")||money_type.equals("1004")||money_type.equals("40")||money_type.equals("50")||money_type.equals("5001"))
		{
			if (cntdevice == 0)
			{
				setMessage("设备购置、改造、项修类项目，必须填写合同设备清单！");
				return false;
			}
		}
		
		return true;
	}

	//查询功能对应的表格
	private Map<String,String> queryTable(String funId) {
		String sql = "select * from fun_base where fun_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(funId);
		
		return _dao.queryMap(param);
	}

	//查询合同信息
	private Map<String,String> queryContract(String contractId,String table_name) {
		StringBuilder sql = new StringBuilder("select * from ");
		sql.append(table_name);
		sql.append(" where contract_id = ? ");
		DaoParam param = _dao.createParam(sql.toString());
		param.addStringValue(contractId);
		
		return _dao.queryMap(param);
	}
	//查询合同设备清单
	private Map<String,String> queryDevice(String contractId) {
		String sql = "select count(*) as cntdevice from contract_device where contract_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(contractId);
		
		return _dao.queryMap(param);
	}
	//查询合同主材清单
	private Map<String,String> querySpare(String contractId) {
		String sql = "select count(*) as cntspare from contract_spare where contract_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(contractId);
		
		return _dao.queryMap(param);
	}
	//查询合同服务清单
	private Map<String,String> queryService(String contractId) {
		String sql = "select count(*) as cntservice from contract_service where contract_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(contractId);
		
		return _dao.queryMap(param);
	}
}
